package happy.diandiankong.api.util;

import java.sql.Time;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Title.
 * <p/>
 * Description.
 *
 * @author Bill Lv {@literal <billcc.lv@hotmail.com>}
 * @version 1.0
 * @since 2015-08-19
 */
public class DateUtil {
    public static final String AM_PM_DATETIME_FORMAT = "MM/dd/yyyy hh:mm a";
    public static final String DEFAULT_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public static final String DEFAULT_TIME_PATTERN = "HH:mm";
    public static final String REMINDER_DT_PATTERN = "yyyyMMddHHmm";
    public static final String DATETIME_WITHOUT_SEC = "yyyy-MM-dd HH:mm";
    public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd";
    public static final String DT_GPIO_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ";

    public static Timestamp parseAMPMFormat(String datetimeString) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(AM_PM_DATETIME_FORMAT);
        final Date date = dateFormat.parse(datetimeString);
        return new Timestamp(date.getTime());
    }

    public static Time parseTime(String time) throws ParseException {
        return new Time(new SimpleDateFormat(DEFAULT_TIME_PATTERN).parse(time).getTime());
    }

    public static String formatTime(Date datetime) {
        return new SimpleDateFormat(DEFAULT_TIME_PATTERN).format(datetime);
    }

    public static String formatGPIODT(Date datetime) {
        return new SimpleDateFormat(DT_GPIO_FORMAT).format(datetime);
    }

    public static boolean isValidAMPMDatetime(String datetimeString) {
        try {
            parseAMPMFormat(datetimeString);
        } catch (ParseException e) {
            return false;
        }
        return true;
    }

    public static Timestamp parseDatetime(String datetimeString) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
        final Date date = dateFormat.parse(datetimeString);
        return new Timestamp(date.getTime());
    }

    public static String formatDatetime(Date datetime) {
        DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATETIME_FORMAT);
        return dateFormat.format(datetime);
    }

    public static String formatDatetimeInAMPMFormat(Date datetime) {
        DateFormat dateFormat = new SimpleDateFormat(AM_PM_DATETIME_FORMAT);
        return dateFormat.format(datetime);
    }

    public static Date parseDatetimeWithoutSeconds(String datetimeString) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(DATETIME_WITHOUT_SEC);
        return dateFormat.parse(datetimeString);
    }

    public static String formatReminderDT(Date datetime) {
        DateFormat dateFormat = new SimpleDateFormat(REMINDER_DT_PATTERN);
        return dateFormat.format(datetime);
    }

    public static String formatDatetimeWithoutSec(Date datetime) {
        DateFormat dateFormat = new SimpleDateFormat(DATETIME_WITHOUT_SEC);
        return dateFormat.format(datetime);
    }

    public static String formatDate(Date date) {
        return new SimpleDateFormat(DEFAULT_DATE_PATTERN).format(date);
    }

    public static Timestamp parseDate(String dateString) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);
        final Date date = dateFormat.parse(dateString);
        return new Timestamp(date.getTime());
    }
}
